 var user=null;
 $(function(){
	
	easyloader.base = '../../js/tools/locale/';
    easyloader.load('easyui-lang-zh_CN.js', function(){     
	 var date=new Date();
	            $.getJSON("../../groupbyajax/user!getSessionUser.action?date="+date.getTime(),function(data){
					user=data.user;
					if(data.user==null)
					{
						alert("请登录！");
					}
				}) ;
				 $.getJSON("../../groupbyajax/user!findCategoryUserList.action?date="+date.getTime(),function(data){
				 	
					if(user==null||user.level==4)
					{
						$("#brandBox").hide();
					}else
					{
						data=data.success;
						var option="<option value='-1'>全部</option>";
						for(var i in data)
						{
							option+="<option value='"+data[i].userId+"'>"+((data[i].username=="")?data[i].eusername:data[i].username)+"</option>";
						}
						$("#brand").html(option);
					}
				}) ;
				$.getJSON("../../groupbyajax/groupby!getCategoryList.action?page=1&rows=10000&flag=3&date="+date.getTime(),function(data){
					
						data=data.rows;
						var option="<option value='-1'>全部</option>";
						for(var i in data)
						{
							option+="<option value='"+data[i].id+"'>"+data[i].title+"</option>";
						}
						$("#category").html(option);
					
				}) ;
				$("#dialog").window({closed: true, modal: false,resizable:false,width:1155,height:528,top:1,left:1});
	    var pram={photoName:"",startDate:"",endDate:"",brandId:-1,category:-1,flag:-1};
		
         $("#dataGrid").datagrid({
				        height: 450,
						width:1140,
                        pagination: true,
						rownumbers:true,
						striped:true,
						pageList:[15,20,25,30],
						pageSize:15,
						showPageList:true,
						singleSelect:true,
						queryParams:pram,
                        url: '../../groupbyajax/brand!pictureList.action?date='+date.getTime(),
                        columns: [[
						          {field: 'name',title: '相册名称',width: 150,align: 'left'},
						          {field: 'description',title: '相册描述',width: 350,align: 'left'},
						          {field: 'datetime',title: '创建时间',width: 120,align: 'center'}, 
								  {field: 'brand',title: '所属品牌',width: 90,align: 'center'},
								  {field: 'category',title: '所属类别',width: 90,align: 'center'},  
								  {field: 'status',title: '是否推荐',width: 80,align: 'left'}, 
								  {field: 'modify',title: '修改',width:80,align: 'center'},
								  {field: 'del',title: '删除',width:80,align: 'center'}
							 ]],
					    onLoadSuccess:function(){
							
							if(user.level==1)
							{
								
								//$("#dataGrid tbody tr").find("td:eq(3)").find("a").addClass("link");
								
							}
							if(user.level==5)
							{
								//$("#dataGrid tbody tr").find("td:eq(3)").find("a").addClass("link");
								
							}
							if(user.level==6)
							{
								//$("#dataGrid tbody tr").find("td:eq(3)").find("a").addClass("link");
                                //$("#dataGrid tbody tr").find("td:eq(4)").find("a").addClass("link");
								
							}
							if(user.level==7)
							{
								//$("#dataGrid tbody tr").find("td:eq(3)").find("a").addClass("link");
                               // $("#dataGrid tbody tr").find("td:eq(4)").find("a").addClass("link");
								
							}
							if(user.level==8)
							{
								//$("#dataGrid tbody tr").find("td:eq(3)").find("a").addClass("link");
                               // $("#dataGrid tbody tr").find("td:eq(4)").find("a").addClass("link");
								
							}
							
							if(user.level==4)
						    {
								hideColumn("status");
							}
							 
						}
			        });		 
			});	
			
	  
});	
function addButton()
{
	
var pager = $('#dataGrid').datagrid('getPager');   
					pager.pagination({
						pageList:[15,20,25,30],
						pageSize:15,
						showPageList:true,
						buttons:[{
							iconCls:'icon-print',
							handler:function(){
								doPrint();
							}
						}]
					});
}
/**
 * 显示
 */
function showColumn(field)
{
	$(".datagrid-view2 .datagrid-header div table tr th").each(function(){
		if($(this).attr("field")==field)
		{
			$(this).show();
			return false;
		}
	});
$(".datagrid-column-"+field).show();
}
/**
 * 隐藏
 * @param {Object} field
 */
function hideColumn(field)
{	
$(".datagrid-view2 .datagrid-header div table tr th").each(function(){
		if($(this).attr("field")==field)
		{
			$(this).hide();
			return false;
		}
	});
$(".datagrid-column-"+field).hide();
}

function ratifyPic(id,able)
{
	var date =new Date();
	$.getJSON("../../groupbyajax/brand!ratifyPic.action?times="+date.getTime(),{picId:id,able:able},function(data){
		if(data.success)
		{
             if(able==1)
			{
				$("#u"+id).parent().html("<a href='javascript:void(0)' onclick='ratifyPic("+id+",0,this);' id='u"+id+"'><font color='blue'>推荐</color></font></a>");
			}else
			{
				$("#u"+id).parent().html("<a href='javascript:void(0)' onclick='ratifyPic("+id+",1,this);' id='u"+id+"'><font color='red'>未推荐</color></font>");
			}

		}
	});
}
function delPictures(id)
{
	$.messager.confirm('删除', '你确定要删除记录吗?', function(r){
		if (r) {
			$.getJSON("../../groupbyajax/brand!delPictures.action", {
				picId: id
			}, function(data){
				if (data.success) {
					$("#dataGrid").datagrid('reload');
				}
			})
		}
	});
	
}
function search()
{
	var name=$("#name").val();
	var startdate=$("#startdate").val();
	var enddate=$("#enddate").val();
	var brand=$("#brand").val();
	var category=$("#category").val();
	if(brand==null)
	{
		brand=-1;
	}
	var recommendation=$("#recommendation").val();
	
	var flag=$("#recommendation").val();
	$('#dataGrid').datagrid({
				  striped: true,
				  queryParams:{photoName:name,startDate:startdate,endDate:enddate,brandId:brand,category:category,flag:recommendation}
			   });
}
function modify(id,obj)
{
	var photoName=$(obj).parent().parent().parent().find(".datagrid-column-name").text();
	var description=$(obj).parent().parent().parent().find(".datagrid-column-description").text();
	
	$("input[name='photoName']").val(photoName);
	$("textarea[name='description']").val(description);
	$("#picId").val(id);
	 $("#dialog").window("open");
}
function modifyTopic()
{
	var picId=$("#picId").val();
	var photoName =$("input[name='photoName']").val();
	var description=$("textarea[name='description']").val();
	if(photoName.length<1||photoName.length>20)
    {
       alert("相册名称的字数在1到20之间");
       return false;
    }
    if(description.length>100)
    {
       alert("相册描述不能大于100");
       return false;
    }
    var date =new Date();
	$.getJSON("../../groupbyajax/brand!modifyPicture.action?times="+date.getTime(),{picId:picId,photoName:photoName,description:description},function(data){
		if(data.success)
		{
			$("#dialog").window("close");
			  $("#dataGrid").datagrid('reload');
		}
	});

}
